package com.arch.bigdata.hbase;

import com.arch.bigdata.hbase.mashibing.Phone;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 如果遇到特别慢的情况，设置DNS服务器地址
 * 223.5.5.5
 * 223.6.6.6
 */
public class HBaseDemo2 {

    Configuration conf = null;
    Connection conn = null;
    // 表的管理对象
    Admin admin = null;
    Table table = null;
    // 创建表的对象
    TableName tableName = TableName.valueOf("t_relation_network_entity_risk_mac");

    @Before
    public void init() throws IOException {
        //创建配置文件对象
        conf = HBaseConfiguration.create();
        //加载zookeeper的配置
        conf.set("hbase.zookeeper.quorum", "1.hadoop3.com,2.hadoop3.com,3.hadoop3.com");
        //获取连接
        conn = ConnectionFactory.createConnection(conf);
        //获取对象
        admin = conn.getAdmin();
        //获取数据操作对象
        table = conn.getTable(tableName);
    }

    @Test
    public void testGetKey() throws IOException {
        Table table = conn.getTable(tableName);
        Scan scan = new Scan();
        scan.setLimit(1);
//        scan.setFilter(new FirstKeyOnlyFilter());
//        scan.setFilter(new PageFilter(1));
        ResultScanner scanner = table.getScanner(scan);
        for (Result rr : scanner) {
            byte[] key = rr.getRow();
            for (Cell cell : rr.listCells()) {
//            Cell cell = rr.listCells().get(0);
                String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                String cf = Bytes.toString(CellUtil.cloneFamily(cell));
                System.out.println(cf + ":::" + qualifier);
            }
            System.out.println("rowkey: " + Bytes.toString(key));
            break;
        }
    }

    @After
    public void destory() {
        try {
            table.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            admin.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            conn.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
